package com.qf.quduba.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.quduba.VO.ResultVO;
import com.qf.quduba.entity.Books;
import com.qf.quduba.service.BookService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author 17312
 * @time2020/10/1410:56
 */
@RestController
@CrossOrigin
@RequestMapping("/es")
public class EsController {
    @Resource
    private BookService bookService;
    @Resource
    private RestHighLevelClient restHighLevelClient;

    @GetMapping("save")
    public ResultVO saveEs(){
        try {
            List<Books> books = bookService.getAllBooks();
            ObjectMapper objectMapper = new ObjectMapper();
            IndexRequest request = new IndexRequest("quduba");
            IndexResponse indexResponse = null;
            for (Books book : books) {
                String jsonStr = objectMapper.writeValueAsString(book);


                request.id(book.getBookId()+"");
                request.source(jsonStr, XContentType.JSON);
                indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);

            }
            if(indexResponse != null){
                return new ResultVO(1000,"success",null);
            }else{
                return new ResultVO(1001,"fail",null);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

    @GetMapping("search")
    public ResultVO searchEs(String keyword){
        try {
            //声明查询条件
            // searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            //MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("bookName", "Java");
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, "bookName", "user.nickname","chapter.chapterName");

            //构造高亮显示字段、样式
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            HighlightBuilder.Field highlightTitle1 = new HighlightBuilder.Field("bookName");
            HighlightBuilder.Field highlightTitle2 = new HighlightBuilder.Field("user.nickname");
            HighlightBuilder.Field highlightTitle3 = new HighlightBuilder.Field("chapter.chapterName");
            highlightBuilder.field(highlightTitle1);
            highlightBuilder.field(highlightTitle2);
            highlightBuilder.field(highlightTitle3);
            highlightBuilder.preTags("<label style='color:red'>");
            highlightBuilder.postTags("</label>");

            //将查询条件及高亮显示  设置到 searchSourceBuilder对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(multiMatchQueryBuilder);
            searchSourceBuilder.highlighter(highlightBuilder);

            //将searchSourceBuilder对象添加到searchRequest
            SearchRequest searchRequest = new SearchRequest("quduba");
            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResp = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

            //处理检索结果
            SearchHits hits = searchResp.getHits();
            SearchHit[] searchHits = hits.getHits();
            if(searchHits != null && searchHits.length > 0){
                return new ResultVO(1000,"success",searchHits);
            }else{
                return new ResultVO(1001,"fail",null);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

}
